
const path = Global.apiPath;

function analyze() {
    fileChooser(null, function (file) {
        var id = uuid();
        let formData = new FormData();
        formData.append("file", file);
        formData.append("id", id);
        let tb = document.getElementById('progressTable');
        tb.innerHTML = `<tr id="${id}"><td>${file.name}</td><td>-</td></tr>`+tb.innerHTML;
        doHttp({
            url: path + "/ncmFile/toMp3",
            body: formData,
            progress: e => {
                var pg = e.loaded / e.total * 100 | 0;
                var nodes = document.getElementById(id).childNodes;
                nodes[1].innerHTML = pg + '%';
                if (pg == 100) getProgress(id);
            },
            success: res => {
                var data = JSON.parse(res);
                if (200 == data.code) {
                    window.location.href = path + "/ncmFile/download?id=" + id;
                }
            }
        });
    });
}

function getProgress(id) {
    doHttp({
        method: 'get',
        url: path + '/ncmFile/toMp3Progress?id=' + id,
        success: function(res) {
            var resp = JSON.parse(res);
            document.getElementById(id).childNodes[1].innerHTML = resp.data;
            switch (resp.code) {
                case 200: getProgress(id); break;
                default: break;
            }
        }
    });
}
